<?php

namespace app\common\service\yunzisheng;

use app\common\service\ApiService;
use app\common\service\HttpService;
use ba\Random;


/*
 * 云知声口语评测service
 * */

class HivoiceEvalService extends ApiService
{
    protected $app_key;
    protected $app_secret;

    protected $evalMp3Url = 'http://edu.hivoice.cn:8085/eval/mp3';

    public function __construct()
    {
        $this->app_key = env('UNISOUND.APP_KEY');
        $this->app_secret = env('UNISOUND.APP_SECRET');
        if (empty($this->app_key) || empty($this->app_secret)) {
            getResponseException('请配置云知声app_key和app_secret', [], 1, "ERROR");
        }
    }

    /*
      * 评测
      * */
    public function evalMp3($text, $voiceUrl)
    {
        $session_id = Random::uuid();
        $headers = [
            "session-id: " . $session_id,
            "appkey: " . $this->app_key . "@" . $this->app_secret
        ];

        $data = [
            'text' => $text,
            'mode' => 'E',
            'voiceurl' => $voiceUrl
        ];
        $ret = HttpService::postRequest(
            $this->evalMp3Url,
            $data,
            $headers
        );
        if (!$ret) {
            logSave([
                'file' => __FILE__,
                'header' => $headers,
                'data' => $data,
                'msg' => '请求失败'
            ]);
            getResponseException('口语测评失败', [], 1, "ERROR");
        }
        return json_decode($ret, true);
    }


}